package com.yh.controller;

import com.yh.entities.Pay;
import com.yh.entities.PayDTO;
import com.yh.resp.ResultData;
import com.yh.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("pay")
@Tag(name = "支付微服务模块", description = "支付CRUD接口")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping(value = "add")
    @Operation(summary = "添加支付记录",description = "新增支付流水方法，返回json串作参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());

        int i = payService.add(pay);

        return ResultData.success("成功插入" + i + "条数据");
    }

    @DeleteMapping(value = "del/{id}")
    @Operation(summary = "删除支付记录",description = "根据id删除支付流水方法")
    public ResultData<Integer> delPay(@PathVariable("id") Integer id){
        int deletionCount = payService.delete(id);

        return ResultData.success(deletionCount);
    }

    @PutMapping(value = "update")
    @Operation(summary = "修改支付流水",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        // 创建一个Pay对象
        Pay pay = new Pay();
        // 将payDTO中的属性复制到pay对象中
        BeanUtils.copyProperties(payDTO, pay);

        // 调用payService的update方法更新pay对象，并返回更新结果
        int i = payService.update(pay);

        return ResultData.success("成功更新" + i + "条数据");
    }


    @GetMapping(value = "find/{id}")
    @Operation(summary = "查询支付流水",description = "根据id查询支付流水方法")
    public ResultData<Pay> findPay(@PathVariable("id") Integer id){
        Pay pay = payService.getById(id);
        if (pay == null) {
            return ResultData.fail("RC404", "没有找到id为" + id + "的支付流水记录");
        } else {
            return ResultData.success(pay);
        }
    }

    @GetMapping(value = "findAll")
    @Operation(summary = "查询所有支付流水",description = "查询所有支付流水方法")
    public ResultData<List<Pay>> findAll(){
        List<Pay> pays = payService.getAll();
        return ResultData.success(pays);
    }

}
